﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.UpgradesUI {
    import flash.display.*;
    import flash.utils.*;
    import org.aswing.*;
    import com.greensock.*;
    import org.aswing.geom.*;
    import org.aswing.event.*;
    import org.aswing.ext.*;
    import Display.aswingui.*;

    public class UpgradesInventoryScrollingList extends JPanel {

        public static const ROW_HEIGHT:int = 104;
        public static const ROW_WIDTH:int = 312;
        public static const ROWS_PER_PAGE:int = 3;
        public static const COLUMNS_PER_PAGE:int = 2;

        public var m_dataList:GridList;
        protected var m_scrollPane:JScrollPane;
        protected var m_cellFactory:GridListCellFactory;
        protected var m_viewport:JViewport;
        protected var m_upBtn:JButton;
        protected var m_downBtn:JButton;
        protected var m_frame:JWindow;
        protected var m_rows:int;
        protected var m_columns:int;
        protected var m_data:Array;
        protected var m_tween:TweenLite;
        protected var m_activeSlots:int = 0;
        protected var curCount:int = 0;
        protected var m_model:VectorListModel;
        protected var m_topCrew:int = 0;

        public function UpgradesInventoryScrollingList(_arg1:Dictionary, _arg2:Array){
            var _local3 = (_arg2.length > (ROWS_PER_PAGE * COLUMNS_PER_PAGE));
            var _local4:int = (_local3) ? FlowLayout.LEFT : FlowLayout.CENTER;
            super(new FlowLayout(_local4, 0, 0));
            var _local5:IntDimension = new IntDimension(ROW_WIDTH, ROW_HEIGHT);
            this.m_cellFactory = new UpgradesInventoryCellFactory(_arg2, _arg1);
            this.m_columns = COLUMNS_PER_PAGE;
            this.m_rows = ROWS_PER_PAGE;
            this.m_data = _arg2;
            this.makeData();
            this.initSize();
            this.append(this.m_scrollPane);
            if (_local3){
                this.append(ASwingHelper.horizontalStrut(10));
                this.append(this.makeNavPane());
            };
            ASwingHelper.prepare(this);
        }
        protected function get TWEEN_TIME():Number{
            return (0.2);
        }
        protected function makeData():void{
            this.m_activeSlots = this.m_data.length;
            this.m_model = new VectorListModel();
            var _local1:int;
            while (_local1 < Math.min(this.m_activeSlots, this.m_data.length)) {
                this.m_model.append(this.m_data[_local1]);
                this.curCount++;
                _local1++;
            };
            while ((this.m_model.size() % COLUMNS_PER_PAGE) > 0) {
                this.m_model.append("empty");
                this.curCount++;
            };
            this.m_dataList = new GridList(this.m_model, this.m_cellFactory, this.m_columns, 0);
            this.m_dataList.setVGap(0);
            this.m_scrollPane = new JScrollPane(this.m_dataList, JScrollPane.SCROLLBAR_NEVER, JScrollPane.SCROLLBAR_NEVER);
        }
        protected function initSize():void{
            this.setPreferredHeight((ROW_HEIGHT * ROWS_PER_PAGE));
            this.m_dataList.setPreferredWidth((ROW_WIDTH * this.m_columns));
            this.m_dataList.setMinimumWidth((ROW_WIDTH * this.m_columns));
            this.m_dataList.setMaximumWidth((ROW_WIDTH * this.m_columns));
            this.m_dataList.setPreferredHeight((ROW_HEIGHT * ROWS_PER_PAGE));
        }
        protected function makeNavPane():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:DisplayObject = (new (UpgradesCommonAssets.assetDict["scrollBorder"])() as DisplayObject);
            var _local3:MarginBackground = new MarginBackground(_local2);
            _local1.setBackgroundDecorator(_local3);
            _local1.setPreferredWidth(_local2.width);
            _local1.setMinimumWidth(_local2.width);
            _local1.setMaximumWidth(_local2.width);
            var _local4:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            _local1.append(this.makeButtons());
            return (_local1);
        }
        protected function makeButtons():JPanel{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            var _local2:DisplayObject = (new (UpgradesCommonAssets.assetDict["scrollCircle"])() as DisplayObject);
            var _local3:AssetPane = new AssetPane(_local2);
            var _local4:JPanel = ASwingHelper.centerElement(_local3);
            var _local5:DisplayObject = (new (UpgradesCommonAssets.assetDict["up_disabled"])() as DisplayObject);
            var _local6:DisplayObject = (new (UpgradesCommonAssets.assetDict["up_over"])() as DisplayObject);
            var _local7:DisplayObject = (new (UpgradesCommonAssets.assetDict["up_up"])() as DisplayObject);
            var _local8:DisplayObject = (new (UpgradesCommonAssets.assetDict["down_disabled"])() as DisplayObject);
            var _local9:DisplayObject = (new (UpgradesCommonAssets.assetDict["down_over"])() as DisplayObject);
            var _local10:DisplayObject = (new (UpgradesCommonAssets.assetDict["down_up"])() as DisplayObject);
            this.m_upBtn = new JButton();
            this.m_upBtn.wrapSimpleButton(new SimpleButton(_local7, _local6, _local5, _local7));
            ASwingHelper.setEasyBorder(this.m_upBtn, 50, 0, 0, 0);
            this.m_downBtn = new JButton();
            this.m_downBtn.wrapSimpleButton(new SimpleButton(_local10, _local9, _local8, _local10));
            this.m_downBtn.addActionListener(this.moveDown, 0, true);
            this.m_upBtn.addActionListener(this.moveUp, 0, true);
            if (this.m_model.size() <= ROWS_PER_PAGE){
                this.m_downBtn.setEnabled(false);
            };
            this.m_upBtn.setEnabled(false);
            _local1.setPreferredHeight((ROW_HEIGHT * ROWS_PER_PAGE));
            _local1.setPreferredWidth(50);
            _local1.appendAll(this.m_upBtn, ASwingHelper.verticalStrut(20), _local4, ASwingHelper.verticalStrut(20), this.m_downBtn);
            return (_local1);
        }
        protected function moveUp(_arg1:AWEvent=null):void{
            if (this.m_topCrew > 0){
                this.setupTween(this.m_topCrew, (this.m_topCrew - 1));
                this.m_topCrew--;
                if (this.m_topCrew == 0){
                    this.m_upBtn.setEnabled(false);
                };
                this.m_downBtn.setEnabled(true);
            };
        }
        protected function moveDown(_arg1:AWEvent=null):void{
            if (this.m_topCrew < (this.m_activeSlots - ROWS_PER_PAGE)){
                this.setupTween(this.m_topCrew, (this.m_topCrew + 1));
                this.m_topCrew++;
                if (this.m_topCrew == (this.m_activeSlots - ROWS_PER_PAGE)){
                    this.m_downBtn.setEnabled(false);
                };
                this.m_upBtn.setEnabled(true);
            };
        }
        protected function setupTween(_arg1:int, _arg2:int):void{
            var startPos:* = 0;
            var destPos:* = 0;
            var tweenObj:* = null;
            var oldIndex:* = _arg1;
            var newIndex:* = _arg2;
            if (((this.m_tween) && (this.m_tween.data))){
                tweenObj = this.m_tween.data;
            } else {
                startPos = (oldIndex * ROW_HEIGHT);
                tweenObj = {
                    currentValue:startPos,
                    lastValue:startPos
                };
            };
            destPos = (newIndex * ROW_HEIGHT);
            var tweenProps:* = {
                currentValue:destPos,
                onStart:function ():void{
                    m_tween.data = tweenObj;
                },
                onUpdate:function ():void{
                    tweenObj.currentValue = Math.ceil(tweenObj.currentValue);
                    m_dataList.scrollVertical((tweenObj.currentValue - tweenObj.lastValue));
                    tweenObj.lastValue = tweenObj.currentValue;
                },
                onComplete:function ():void{
                    m_dataList.scrollVertical((destPos - tweenObj.lastValue));
                    m_tween = null;
                }
            };
            this.m_tween = TweenLite.to(tweenObj, this.TWEEN_TIME, tweenProps);
        }
        public function rebuildCellsAfterBuyAll():void{
            var _local2:UpgradeInventoryCell;
            var _local1:int;
            while (_local1 < this.curCount) {
                _local2 = (this.m_dataList.getCellByIndex(_local1).getCellComponent() as UpgradeInventoryCell);
                _local2.performUpdate();
                _local1++;
            };
        }

    }
}//package Display.UpgradesUI 
